using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Zephyris_Space_Mercs
{
    class HUD
    {
        int timeCounter = 0;

        List<ClickableRegion> clickableRegions = new List<ClickableRegion>();
        private SpriteFont font;

        public HUD(SpriteFont HUD_Font)
        {
            this.font = HUD_Font;
        }

        public void UpdateHUD()
        {
        }

        public void DrawHUDReticule(SpriteBatch spriteBatch, Texture2D reticule, Viewport viewport)
        {
            Vector2 screenSize = new Vector2(viewport.Width, viewport.Height);

            spriteBatch.Draw(
                    reticule,
                    (screenSize / 2) - new Vector2(reticule.Width / 2f, reticule.Height / 2f),
                    Color.Aqua);
        }

        public void DrawHUDText(SpriteBatch spriteBatch, string text, Vector2 position)
        {
            spriteBatch.DrawString(this.font, text, position, Color.Aqua);
        }

        public void DrawHUDUI(SpriteBatch spriteBatch, Texture2D hudButton, Vector2 position)
        {
            timeCounter++;
            if (timeCounter > Constants.timeCounterVal)
                timeCounter = 0;

            clickableRegions.Clear();

            Vector2 topleft = new Vector2(0, 0);
            Vector2 bottomright = new Vector2(0, 0);

            topleft = position;
            bottomright = new Vector2(topleft.X + hudButton.Width, topleft.Y + hudButton.Height);
            spriteBatch.Draw(hudButton, topleft, new Color(255, 255, 255, 255));
            AddClickable(topleft, bottomright);
        }

        public void DrawHUDHitIndicator(SpriteBatch spriteBatch, Texture2D hitindicator, Viewport viewport)
        {
            Vector2 screenSize = new Vector2(viewport.Width, viewport.Height);

            Color color = new Color(1.0f, 1.0f, 1.0f, 0.02f);

            spriteBatch.Draw(hitindicator, new Vector2(0.0f, 0.0f), Color.White);
        }

        public void DrawHUDShieldBar(SpriteBatch spriteBatch, Texture2D valuebar, Viewport viewport, float shieldvalue, float maxshieldvalue)
        {
            //shield
            spriteBatch.Draw(
                valuebar,
                new Rectangle(viewport.Width / 2 - valuebar.Width / 8, (viewport.Height - valuebar.Height) - 1, valuebar.Width / 4, 15),
                new Rectangle(0, 48, valuebar.Width / 4, 11),
                Color.Gray
                );

            spriteBatch.Draw(
               valuebar,
               new Rectangle(viewport.Width / 2 - valuebar.Width / 8, (viewport.Height - valuebar.Height) - 1, (valuebar.Width * (int)((shieldvalue / maxshieldvalue) * 100)) / 400, 15),
               new Rectangle(0, 48, valuebar.Width / 4, 11),
               Color.Blue
               );

            spriteBatch.Draw(
                valuebar,
                new Rectangle(viewport.Width / 2 - valuebar.Width / 8, viewport.Height - valuebar.Height, valuebar.Width / 4, 15),
                new Rectangle(0, 0, valuebar.Width, 48),
                Color.Black
                );
            //end shield
        }

        public void DrawHUDArmourBar(SpriteBatch spriteBatch, Texture2D valuebar, Viewport viewport, float armourvalue, float maxarmourvalue)
        {
            //armour
            spriteBatch.Draw(
                valuebar,
                new Rectangle(viewport.Width / 2 - valuebar.Width / 8, (viewport.Height - valuebar.Height) + 17, valuebar.Width / 4, 15),
                new Rectangle(0, 48, valuebar.Width / 4, 11),
                Color.Gray
                );

            spriteBatch.Draw(
                valuebar,
                new Rectangle(viewport.Width / 2 - valuebar.Width / 8, (viewport.Height - valuebar.Height) + 17, (valuebar.Width * (int)((armourvalue / maxarmourvalue) * 100)) / 400, 15),
                new Rectangle(0, 48, valuebar.Width / 4, 11),
                Color.Red
                );

            spriteBatch.Draw(
                valuebar,
                new Rectangle(viewport.Width / 2 - valuebar.Width / 8, (viewport.Height - valuebar.Height) + 18, valuebar.Width / 4, 15),
                new Rectangle(0, 0, valuebar.Width, 48),
                Color.Black
                );
            //end armour
        }
        public void AddClickable(Vector2 topleftCorner, Vector2 bottomrightCorner)
        {
            ClickableRegion clickableRegion = new ClickableRegion();
            clickableRegion.TopLeftCorner = topleftCorner;
            clickableRegion.BottomRightCorner = bottomrightCorner;
            clickableRegions.Add(clickableRegion);
        }

        public void CheckClick(Vector2 clickedRegion)
        {
            if (timeCounter > Constants.timeCounterVal / 2)
            {
                for (int i = 0; i < clickableRegions.Count; i++)
                {
                    switch (i)
                    {
                        case 0:
                            if ((clickedRegion.X > clickableRegions[0].TopLeftCorner.X) && (clickedRegion.X < clickableRegions[0].BottomRightCorner.X)
                                &&
                                (clickedRegion.Y > clickableRegions[0].TopLeftCorner.Y) && (clickedRegion.Y < clickableRegions[0].BottomRightCorner.Y))
                            {
                                timeCounter = 0;

                                Game1.UpdateBullet(BulletObject.BulletType.plasma);
                            }
                            break;
                    }
                }
            }
        }
    }
}
